package com.hnust.service.impl;

import com.hnust.dao.AuditDao;
import com.hnust.dao.TeaCourseGroupDao;
import com.hnust.domain.AuditJoin;
import com.hnust.domain.TeaCourseGroup;
import com.hnust.domain.record.AuditJoinRecord;
import com.hnust.domain.record.AuditRegisterRecord;
import com.hnust.service.AuditService;
import com.hnust.util.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 创建人：gh
 * 创建时间：2020/10/6
 * 描述：审核注册、审核加入service
 */
@Service("auditService")
public class AuditServiceImpl implements AuditService {

    @Autowired
    AuditDao auditDao; // 操作数据库

    @Autowired
    TeaCourseGroupDao teaCourseGroupDao;


    // 点击审核加入时，返回审核信息
    @Override
    public List<AuditJoinRecord> auditJoinGet(Integer currentPage, Integer rows, String collegeId) {
        return auditDao.auditJoinGet(collegeId, rows, (currentPage - 1) * rows);
    }

    //通过或者驳回审核加入
    @Override
    public boolean auditJoinOperate(String auditJoinId, Integer result) {
        if(result==2){
            AuditJoin auditJoin = auditDao.getAuditJoin(auditJoinId);
            teaCourseGroupDao.addTeaCourseGroup(new TeaCourseGroup(IDUtil.getPrimaryKey(),0,auditJoin.getTeaId(),auditJoin.getCouGroId()));
        }
        return auditDao.auditJoinOperate(auditJoinId, result) == 1;
    }

    //批量通过或者驳回审核加入
    @Override
    public boolean auditBatchJoinOperate(List<String> auditJoinIds, Integer result) {
        boolean res = true;
        for (String auditJoinId : auditJoinIds) {
            if(!auditJoinOperate(auditJoinId, result)){
                res = false;
                break;
            }
        }
        return res;
    }

    // 点击审核注册时，返回审核信息
    @Override
    public List<AuditRegisterRecord> auditRegisterGet(Integer currentPage, Integer rows, String collegeId) {
        return auditDao.auditRegisterGet(collegeId, rows, (currentPage - 1) * rows);
    }

    // 通过注册/驳回注册
    @Override
    public boolean auditRegisterOperate(String auditRegisterId, Integer result) {
        return auditDao.auditRegisterOperate(auditRegisterId, result) == 1;
    }

    // 批量通过注册/批量驳回注册
    @Override
    public boolean auditBatchRegisterOperate(List<String> auditRegisterIds, Integer result) {
        for(String auditRegisterId: auditRegisterIds){
            if(auditDao.auditRegisterOperate(auditRegisterId, result) != 1){
                return false;
            }
        }
        return true;
    }
}
